package com.xmtlxx.web.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.xmtlxx.common.annotation.Log;
import com.xmtlxx.common.annotation.RepeatSubmit;
import com.xmtlxx.common.core.controller.BaseController;
import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.core.validate.AddGroup;
import com.xmtlxx.common.core.validate.EditGroup;
import com.xmtlxx.common.core.validate.QueryGroup;
import com.xmtlxx.common.enums.BusinessType;
import com.xmtlxx.common.utils.poi.ExcelUtil;
import com.xmtlxx.system.domain.bo.SysFormBo;
import com.xmtlxx.system.domain.vo.SysFormVo;
import com.xmtlxx.system.service.ISysFormService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 业务单Controller
 *
 * @author xmtlxx
 * @date 2022-04-24
 */
@Validated
@Api(value = "业务单控制器", tags = {"业务单管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/sysForm")
public class SysFormController extends BaseController {

    private final ISysFormService iSysFormService;

    /**
     * 返回一个初始化的对象到前端，方便进行动态渲染
     */
    @ApiOperation("用于返回一个初始化对象")
    @PostMapping("/initBean")
    R<SysFormVo> initBean(){
        SysFormVo vo = new SysFormVo();
        //可以对数据进行初始化
        return R.ok(vo);
    }

    /**
     * 查询业务单列表
     */
    @ApiOperation("查询业务单列表")
    @SaCheckPermission("system:sysForm:list")
    @GetMapping("/list")
    public R<List<SysFormVo>> list(@Validated(QueryGroup.class) SysFormBo bo) {
        return R.ok(iSysFormService.queryList(bo));
    }

    /**
     * 导出业务单列表
     */
    @ApiOperation("导出业务单列表")
    @SaCheckPermission("system:sysForm:export")
    @Log(title = "业务单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated SysFormBo bo, HttpServletResponse response) {
        List<SysFormVo> list = iSysFormService.queryList(bo);
        ExcelUtil.exportExcel(list, "业务单", SysFormVo.class, response);
    }

    /**
     * 获取业务单详细信息
     */
    @ApiOperation("获取业务单详细信息")
    @SaCheckPermission("system:sysForm:query")
    @GetMapping("/{formid}")
    public R<SysFormVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("formid") String formid) {
        return R.ok(iSysFormService.queryById(formid));
    }

    /**
     * 新增业务单
     */
    @ApiOperation("新增业务单")
    @SaCheckPermission("system:sysForm:add")
    @Log(title = "业务单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysFormVo vo) {
        SysFormBo bo = new SysFormBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iSysFormService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改业务单
     */
    @ApiOperation("修改业务单")
    @SaCheckPermission("system:sysForm:edit")
    @Log(title = "业务单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysFormVo vo) {
        SysFormBo bo = new SysFormBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iSysFormService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除业务单
     */
    @ApiOperation("删除业务单")
    @SaCheckPermission("system:sysForm:remove")
    @Log(title = "业务单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{formids}")
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable String[] formids) {
        return toAjax(iSysFormService.deleteWithValidByIds(Arrays.asList(formids), true) ? 1 : 0);
    }
}
